import WKSDK, { ConnectStatus } from 'wukongimjssdk'
//二次封装WKSDK
class CustomWKSDK implements UTILS.WKIMSDK {
  constructor() {
    console.log('中源来 WKIMSDK installed')
  }
  isConnected = false
  public init(token: string, uid: string): void {
    if (this.isConnected) return console.log('WKIM already connected')
    // 配置
    console.log('try ro connect WKIM')
    WKSDK.shared().config.addr = import.meta.env.VITE_WEBSOCKET_URL
    WKSDK.shared().config.uid = uid
    WKSDK.shared().config.token = token
    // 连接
    WKSDK.shared().connectManager.connect()
    // 监听连接状态
    WKSDK.shared().connectManager.addConnectStatusListener(
      (status: ConnectStatus, reasonCode?: number) => {
        if (status === ConnectStatus.Connected) {
          this.isConnected = true
          console.log('WKIM:connect success')
        } else {
          console.error('WKIM:connect fail, reasonCode:', reasonCode)
          this.isConnected = false
        }
      },
    )
  }
  public connect(): void {
    if (this.isConnected) return
    WKSDK.shared().connectManager.connect()
  }
  public disconnect(): void {
    if (!this.isConnected) return
    WKSDK.shared().connectManager.disconnect()
    this.isConnected = false
  }
}
export let ZYL_WKSDK: UTILS.WKIMSDK | null = null
export default {
  install() {
    if (!ZYL_WKSDK) {
      ZYL_WKSDK = new CustomWKSDK()
    }
  },
}
